package com.example.zyfyptlw618.model;

import com.example.zyfyptlw618.common.Common;
import com.example.zyfyptlw618.iface.LoginInputIface;
import com.example.zyfyptlw618.iface.LoginInputListener;
import com.example.zyfyptlw618.service.LoginInputService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class LoginInputModel implements LoginInputIface {
    private Retrofit retrofit;

    public LoginInputModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    @Override
    public void getLoginInputResult(String username, String password, String tel, final LoginInputListener listener) {
        LoginInputService regService=retrofit.create(LoginInputService.class);
        Call<String> call=regService.loginInput(username,password,tel);

        call.enqueue(new Callback<String>(){

            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()&&response.body()!=null&&response.body().equals("1"))
                {
                    listener.onResponse(response.body());
                }
                else listener.onFail("register fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.getMessage ());
            }
        });
    }

}
